package com.baize.minespace.utils;

import com.baize.minespace.config.MailConfig;
import com.baize.minespace.pojo.Mail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class MailUtils {
    private static MimeMessage message;
    public static Result<String> sendMail(Mail mail){
        Properties props = MailConfig.getProperties();
        Session session = Session.getDefaultInstance(props,  new Authenticator() {
            //身份认证
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MailConfig.username, MailConfig.password);
            }
        });
        try {
            InternetAddress[] addresses = {new InternetAddress(MailConfig.to)};
            message = new MimeMessage(session);
            message.setFrom(new InternetAddress(MailConfig.from));
            message.setRecipients(Message.RecipientType.TO,addresses);
            message.setSubject(mail.getSubject());//发送标题（自定义）
            message.setSentDate(new Date());
            message.setText(mail.getContent());//发送内容（自定义）
            Transport transport = session.getTransport("smtp");
            transport.connect(MailConfig.smtpHost, MailConfig.username, MailConfig.password);
            transport.send(message);
            return Result.success("发送成功");
        }catch (Exception e) {
            return Result.fail(e.getMessage()+"发送失败");
        }
    }

    public static void main(String[] args) {
        int a = 9;
        int b = a++;
        int c = ++a;
        int d = c--;
        int e = --d;
        System.out.printf(a + ":" + b + ":" + c + ":" + d + ":" + e);
    }
}